package com.lyf.boot.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lyf.boot.annotation.Log;
import com.lyf.boot.core.model.IdArrDto;
import com.lyf.boot.core.model.IdDto;
import com.lyf.boot.core.model.PageInfo;
import com.lyf.boot.core.model.R;
import com.lyf.boot.enums.BusinessType;
import com.lyf.boot.modules.system.entity.dto.CheckSysUserDto;
import com.lyf.boot.modules.system.entity.dto.SysUserDto;
import com.lyf.boot.modules.system.entity.dto.SysUserPageDto;
import com.lyf.boot.modules.system.entity.vo.SysUserVo;
import com.lyf.boot.modules.system.service.ISysUserService;
import com.lyf.boot.validate.group.AddGroup;
import com.lyf.boot.validate.group.EditGroup;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统用户管理接口
 * @author Luyufan
 * @date 2023/7/5 18:31
 */
@RestController
@RequestMapping("/sysUser")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService sysUserService;

    /**
     * 用户分页
     * @param dto
     * @return
     */
    @PostMapping("/getSysUserPage")
    @SaCheckPermission("sys:user:list")
    public R<PageInfo<SysUserVo>> getSysUserPage(@RequestBody SysUserPageDto dto){
        return R.ok(sysUserService.getSysUserPage(dto));
    }

    /**
     * 查询用户详情
     * @param dto
     * @return
     */
    @PostMapping("/getSysUserInfo")
    public R<SysUserVo> getSysUserInfo(@RequestBody @Valid IdDto dto){
        return R.ok(sysUserService.getSysUserInfo(dto.getId()));
    }

    /**
     * 验证账号、手机号、邮箱是否已存在
     * @param dto
     * @return
     */
    @PostMapping("/checkSysUserExist")
    public R<Boolean> checkSysUserExist(@RequestBody CheckSysUserDto dto){
        return R.ok(sysUserService.checkSysUserExist(dto));
    }

    /**
     * 新增用户
     * @param dto
     * @return
     */
    @PostMapping("/saveSysUser")
    @SaCheckPermission("sys:user:add")
    @Log(module = "用户管理", business = "新增用户", businessType = BusinessType.INSERT)
    public R<Void> saveSysUser(@RequestBody @Validated(AddGroup.class)SysUserDto dto){
        sysUserService.saveSysUser(dto);
        return R.ok();
    }

    /**
     * 修改用户
     * @param dto
     * @return
     */
    @PostMapping("/updateSysUser")
    @SaCheckPermission("sys:user:update")
    @Log(module = "用户管理", business = "修改用户", businessType = BusinessType.UPDATE)
    public R<Void> updateSysUser(@RequestBody @Validated(EditGroup.class) SysUserDto dto){
        sysUserService.updateSysUser(dto);
        return R.ok();
    }

    /**
     * 删除用户
     * @param dto
     * @return
     */
    @PostMapping("/deleteSysUser")
    @SaCheckPermission("sys:user:delete")
    @Log(module = "用户管理", business = "删除用户", businessType = BusinessType.DELETE)
    public R<Void> deleteSysUser(@RequestBody @Valid IdArrDto dto){
        sysUserService.deleteSysUser(dto.getIds());
        return R.ok();
    }

    /**
     * 重置密码
     * @author Luyufan
     * @date 2023/8/14 16:05
     * @param dto
     * @return R<Void>
     */
    @PostMapping("/resetPassword")
    @SaCheckPermission("sys:user:resetPwd")
    @Log(module = "用户管理", business = "重置密码", businessType = BusinessType.OTHER)
    public R<Void> resetPassword(@RequestBody @Valid IdDto dto){
        sysUserService.resetPassword(dto);
        return R.ok();
    }
}
